package cn.ym.mall.service.impl;

import cn.ym.mall.dao.ActiveTypeDao;
import cn.ym.mall.dao.impl.ActiveTypeDaoImpl;
import cn.ym.mall.entity.ActiveJoin;
import cn.ym.mall.entity.ActiveType;
import cn.ym.mall.service.ActiveTypeService;
import cn.ym.mall.utils.DruidUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 漆家乐
 */
public class ActiveTypeServiceImpl implements ActiveTypeService {
    ActiveTypeDao activeTypeDao = new ActiveTypeDaoImpl();
    /**
     * 添加活动类型
     *
     * @param activeType
     * @return
     * @throws SQLException
     */
    @Override
    public boolean add(ActiveType activeType) throws SQLException {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if (activeTypeDao.add(activeType) == 1) {
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 删除活动类型
     *
     * @param aid
     * @return
     * @throws SQLException
     */
    @Override
    public boolean delete(Integer aid) throws SQLException {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if (activeTypeDao.delete(aid) == 1) {
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 修改活动类型
     *
     * @param activeType
     * @return
     * @throws SQLException
     */
    @Override
    public boolean update(ActiveType activeType) throws SQLException {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if (activeTypeDao.update(activeType) == 1) {
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 查询所有活动类型
     *
     * @return
     * @throws SQLException
     */
    @Override
    public List<ActiveType> getAll() throws SQLException {
        Connection conn = null;
        List<ActiveType> activeTypeList = new ArrayList<>();
        try {
            conn = DruidUtils.getConnection();
            activeTypeList = activeTypeDao.getAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return activeTypeList;
    }

    /**
     * 根据id查询活动类型
     *
     * @param actid
     * @return
     * @throws SQLException
     */
    @Override
    public ActiveType getActiveTypeById(Integer actid) throws SQLException {
        Connection conn = null;
        ActiveType activeType = null;
        try {
            conn = DruidUtils.getConnection();
            activeType = activeTypeDao.getActiveTypeById(actid);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return activeType;
    }

    /**
     * 根据活动名查询活动类型
     *
     * @param actname
     * @return
     * @throws SQLException
     */
    @Override
    public ActiveType getActiveTypeByType(String actname) throws SQLException {
        Connection conn = null;
        ActiveType activeType = null;
        try {
            conn = DruidUtils.getConnection();
            activeType = activeTypeDao.getActiveTypeByType(actname);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return activeType;
    }
}
